package nio;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * description:
 * author:zt
 * date:2021-09-10
 */
public class NIOFileChannel01 {
    public static void main(String[] args) throws IOException {
        String str = "hello";
        //创建输出流
        FileOutputStream fileOutputStream = new FileOutputStream("d:\\file01.txt");

        //通过fileOutputStream获取对应的FileChannel
        FileChannel channel = fileOutputStream.getChannel();
        //创建一个缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        //将str放入buffer
        byteBuffer.put(str.getBytes());
        //对buffer进行读写反转
        byteBuffer.flip();

        //将buffer数据写入到channel
        channel.write(byteBuffer);
        fileOutputStream.close();
    }
}
